using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.CompositeUI.WinForms.UIElements;
using System.Windows.Forms;

namespace SCSFContrib.CompositeUI.WinForms.UIElements
{
    /// <summary>
    /// Provides an adapter for ToolStripItems where new items will be added to the separator's owner collection, 
    /// after the separator to which the adapter is attached. The separator is hidden/shown in respect to the
    /// ToolStripItems that follows the separator.
    /// </summary>
    public class AfterSeparatorUIAdapter : ToolStripItemOwnerCollectionUIAdapter
    {
        ToolStripSeparator separator;

        /// <summary>
        /// Initializes a new instance of the <see cref="AfterSeparatorUIAdapter"/> using the
        /// specified separator.
        /// </summary>
        public AfterSeparatorUIAdapter(ToolStripSeparator separator)
            : base(separator)
        {
            this.separator = separator;
            UpdateUI();
        }

        protected override ToolStripItem Add(ToolStripItem uiElement)
        {
            ToolStripItem item = base.Add(uiElement);
            UpdateUI();
            return item;
        }

        protected override void Remove(ToolStripItem uiElement)
        {
            base.Remove(uiElement);
            UpdateUI();
        }

        private void UpdateUI()
        {
            if (InternalCollection != null)
            {
                separator.Visible = (InternalCollection.IndexOf(separator) != InternalCollection.Count - 1);
            }
            else
            {
                separator.Visible = false;
            }
        }
    }
}
